<template>
  <div class="gather">
    <div v-for="(item, index) in gathers" :key="index" class="gather-card" :class="{ 'primary-card': index === 0 }"
      :style="cardStyle(item)">
      <div class="gather-card__content">
        <span class="gather-card__label">{{ item.name }}</span>
        <div class="gather-card__number">{{ item.total }}</div>
      </div>
      <div class="gather-card__icon" v-if="index > 0" @click.stop="handleIconClick(item.path, $event)">
        <IconPlusOutline @click.stop="handleIconClick(item.path, $event)" />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { overLink } from '@/utils/menu';
import { useRouter } from 'vue-router';
import { overviewAPI } from '@/api';
import { useUserStore } from '@/stores/user';
import { useCode } from '@/hooks/code';

const userStore = useUserStore()
const { tackleCode } = useCode()
const router = useRouter()
const gathers = ref(JSON.parse(JSON.stringify(overLink))) // 深拷贝防止修改原始数据
console.log('111', gathers.value);

// 预定义一组好看的渐变色组合
const gradientPresets = [
  // 蓝紫系列
  '135deg, #4776E6, #8E54E9',
  '120deg, #0061ff, #60efff',
  '135deg, #13547a, #80d0c7',

  // 红橙系列
  '120deg, #ff512f, #dd2476',
  '135deg, #ff9966, #ff5e62',

  // 绿色系列
  '120deg, #11998e, #38ef7d',
  '135deg, #56ab2f, #a8e063',

  // 紫色系列
  '135deg, #834d9b, #d04ed6',
  '120deg, #6a3093, #a044ff',

  // 黄橙系列
  '135deg, #f46b45, #eea849',
  '120deg, #fcb045, #fd1d1d',

  // 深蓝系列
  '135deg, #396afc, #2948ff',
  '120deg, #4568dc, #b06ab3'
]

// 生成随机渐变色

// 创建卡片色彩组合
const assignRandomColors = () => {
  // 确保四个卡片用不同的颜色
  const usedIndices = new Set()
  const totalPresets = gradientPresets.length

  gathers.value.forEach((item: any) => {
    let randomIndex
    // 确保不重复选择相同的渐变
    do {
      randomIndex = Math.floor(Math.random() * totalPresets)
    } while (usedIndices.has(randomIndex) && usedIndices.size < totalPresets)

    usedIndices.add(randomIndex)
    item.bgColor = gradientPresets[randomIndex]
  })
}

// 计算卡片样式
const cardStyle = (item: any) => {
  return {
    background: item.bgColor.includes(',')
      ? `linear-gradient(${item.bgColor})`
      : item.bgColor
  }
}

// 获取数据
const drawGatherData = async () => {
  const request = {
    token: userStore.token
  }
  const res: any = await overviewAPI(request)
  if (tackleCode(res.code)) {
    const data = res.data
    gathers.value[0].total = data.file
    gathers.value[1].total = data.article
    gathers.value[2].total = data.gallery
    gathers.value[3].total = data.diary
  }
}

const editPage = (path: string) => {
  console.log(111);

  console.log('跳转到编辑页面', path);

  router.push({
    path: path
  })
}

const handleIconClick = (path: any, event: any) => {
  event.stopPropagation();
  console.log('点击事件捕获，path:', path);
  editPage(path);
}

onMounted(() => {
  // 分配随机颜色
  assignRandomColors()
  // 获取统计数据
  drawGatherData()
})
</script>

<style lang="less" scoped>
// 颜色变量
@card-shadow: 0 10px 20px rgba(0, 0, 0, 0.06);
@card-hover-shadow: 0 12px 28px rgba(0, 0, 0, 0.12);
@transition-time: 0.3s;
@transition-function: cubic-bezier(0.25, 1, 0.5, 1);

// 网格布局容器
.gather {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 24px;
  width: 100%;
  margin-bottom: 40px;
}

// 卡片基本样式
.gather-card {
  position: relative;
  padding: 28px 24px;
  border-radius: 16px;
  color: #fff;
  box-shadow: @card-shadow;
  overflow: hidden;
  transition: all @transition-time @transition-function;
  display: flex;
  justify-content: space-between;
  align-items: center;
  min-height: 150px;
  backdrop-filter: blur(5px);
  border: 1px solid rgba(255, 255, 255, 0.1);

  // 鼠标悬停效果
  &:hover {
    transform: translateY(-6px);
    box-shadow: @card-hover-shadow;

    .gather-card__icon {
      transform: scale(1.08) rotate(5deg);
      background-color: rgba(255, 255, 255, 0.25);
    }

    .gather-card__number {
      transform: scale(1.05);
    }
  }

  // 卡片内容区
  &__content {
    z-index: 2;
    display: flex;
    flex-direction: column;
    gap: 12px;
  }

  // 标签文字
  &__label {
    font-size: 16px;
    font-weight: 600;
    opacity: 0.9;
    letter-spacing: 0.4px;
    text-shadow: 0 1px 3px rgba(0, 0, 0, 0.15);
  }

  // 数字统计
  &__number {
    font-size: 38px;
    font-weight: 700;
    margin-top: 4px;
    letter-spacing: -0.5px;
    transition: transform 0.4s @transition-function;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  }

  // 图标容器
  &__icon {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background-color: rgba(255, 255, 255, 0.15);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.35s @transition-function;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    position: relative;
    z-index: 10;
    /* 提高z-index确保在最上层 */

    /* 修复SVG事件问题 */
    svg,
    path {
      pointer-events: none;
    }

    &:active {
      transform: scale(0.95);
    }

    svg {
      width: 22px;
      height: 22px;
      color: #fff;
    }
  }

  // 主要卡片的额外样式
  &.primary-card {
    position: relative;

    &::after {
      content: '';
      position: absolute;
      top: -40%;
      right: -30%;
      width: 220px;
      height: 220px;
      border-radius: 50%;
      background: linear-gradient(120deg, rgba(255, 255, 255, 0.15), rgba(255, 255, 255, 0));
      z-index: 1;
    }

    &::before {
      content: '';
      position: absolute;
      bottom: -25%;
      left: -10%;
      width: 160px;
      height: 160px;
      border-radius: 50%;
      background: linear-gradient(220deg, rgba(255, 255, 255, 0.12), rgba(255, 255, 255, 0));
      z-index: 1;
    }
  }

  // 添加闪亮效果
  &:nth-child(even)::after {
    content: '';
    position: absolute;
    top: -20px;
    right: -20px;
    width: 100px;
    height: 100px;
    border-radius: 50%;
    background: radial-gradient(rgba(255, 255, 255, 0.2), transparent 70%);
    z-index: 1;
  }

  // 为所有卡片添加微妙的光晕效果
  &::before {
    content: '';
    position: absolute;
    inset: 0;
    background: radial-gradient(circle at 50% 0%,
        rgba(255, 255, 255, 0.1),
        transparent 70%);
    z-index: 1;
  }
}

// 响应式布局
@media (min-width: 1400px) {
  .gather {
    grid-template-columns: repeat(4, 1fr);
  }
}

@media (max-width: 1200px) and (min-width: 769px) {
  .gather {
    grid-template-columns: repeat(2, 1fr);
    gap: 20px;
  }
}

@media (max-width: 768px) {
  .gather {
    grid-template-columns: repeat(2, 1fr);
    gap: 16px;
  }

  .gather-card {
    padding: 20px;
    min-height: 130px;

    &__label {
      font-size: 14px;
    }

    &__number {
      font-size: 28px;
    }

    &__icon {
      width: 40px;
      height: 40px;

      svg {
        width: 18px;
        height: 18px;
      }
    }
  }
}

@media (max-width: 540px) {
  .gather {
    grid-template-columns: 1fr;
    gap: 18px;
  }

  .gather-card {
    min-height: 110px;

    &__content {
      gap: 8px;
    }

    &__number {
      font-size: 32px; // 在单列显示时稍微增大数字大小，增强视觉焦点
    }
  }
}
</style>
